// import parseTime, formatTime and set to filter
export { parseTime, formatTime, formatRMB } from '@/utils'
import moment from 'moment'

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function filterType(value, arr) {
  const info = arr.find(item => item.value === value)
  if (info) {
    return info.name
  } else {
    return ''
  }
}
export function filterYear(value) {
  return moment(value).format('YYYY')
}

export function filterTime(value, format) {
  if (value) {
    return moment(value).format(format)
  } else {
    return ''
  }
}

export function filterColor(value, arr) {
  const info = arr.find(item => item.value === value)
  if (info) {
    if (info.dictColor) {
      return 'border: 1px solid' + info.dictColor + ';color:' + info.dictColor
    } else {
      return ''
    }
  } else {
    return ''
  }
}
export function filterClassColor(value, arr) {
  const info = arr.find(item => item.value === value)
  if (info) {
    if (info.dictColor) {
      return 'el-tag el-tag--plain'
    } else {
      return ''
    }
  } else {
    return ''
  }
}
/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}
export function changeMoneyToChinese(money) {
  var cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] // 汉字的数字
  var cnIntRadice = ['', '拾', '佰', '仟'] // 基本单位
  var cnIntUnits = ['', '万', '亿', '兆'] // 对应整数部分扩展单位
  var cnDecUnits = ['角', '分', '毫', '厘'] // 对应小数部分单位
  var cnInteger = '整' // 整数金额时后面跟的字符
  var cnIntLast = '元' // 整型完以后的单位
  var maxNum = 999999999999999.9999 // 最大处理的数字

  var IntegerNum // 金额整数部分
  var DecimalNum // 金额小数部分
  var ChineseStr = '' // 输出的中文金额字符串
  var parts // 分离金额后用的数组，预定义

  if (money === '') {
    return ''
  }

  money = parseFloat(money)
  // alert(money);
  if (money >= maxNum) {
    return ''
  }
  if (money === 0) {
    ChineseStr = cnNums[0] + cnIntLast + cnInteger
    // document.getElementById("show").value=ChineseStr;
    return ChineseStr
  }
  money = money.toString() // 转换为字符串
  if (money.indexOf('.') === -1) {
    IntegerNum = money
    DecimalNum = ''
  } else {
    parts = money.split('.')
    IntegerNum = parts[0]
    DecimalNum = parts[1].substr(0, 4)
  }
  if (parseInt(IntegerNum, 10) > 0) { // 获取整型部分转换
    let zeroCount = 0
    const IntLen = IntegerNum.length
    for (let i = 0; i < IntLen; i++) {
      const n = IntegerNum.substr(i, 1)
      const p = IntLen - i - 1
      const q = p / 4
      const m = p % 4
      if (n === '0') {
        zeroCount++
      } else {
        if (zeroCount > 0) {
          ChineseStr += cnNums[0]
        }
        zeroCount = 0 // 归零
        ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
      }
      if (m === 0 && zeroCount < 4) {
        ChineseStr += cnIntUnits[q]
      }
    }
    ChineseStr += cnIntLast
    // 整型部分处理完毕
  }
  if (DecimalNum !== '') { // 小数部分
    const decLen = DecimalNum.length
    for (let i = 0; i < decLen; i++) {
      const n = DecimalNum.substr(i, 1)
      if (n !== '0') {
        ChineseStr += cnNums[Number(n)] + cnDecUnits[i]
      }
    }
  }
  if (ChineseStr === '') {
    ChineseStr += cnNums[0] + cnIntLast + cnInteger
  } else if (DecimalNum === '') {
    ChineseStr += cnInteger
  }
  return ChineseStr
}
